﻿Imports System.Runtime.InteropServices
Imports System.ComponentModel

''' <summary>
''' This Class Provides the TreeView with extended functionalities like,
''' Adding the HeaderText feature to Node, Setting the icon for TreeViewNode.
''' </summary>

Public Class TreeViewItemWithIcons
    Inherits TreeViewItem

    Public tagXML As XElement
#Region "Global variables"
    Private iconSource As ImageSource
    Private textBlock As TextBlock
    Private m_icon As Image
#End Region

#Region "Constructors and Destructors"
    Public Sub New()
        Dim stack As New StackPanel()
        stack.Orientation = Orientation.Horizontal
        Header = stack
        'Uncomment this code If you want to add an Image after the Node-HeaderText
        'textBlock = new TextBlock();
        'textBlock.VerticalAlignment = VerticalAlignment.Center;
        'stack.Children.Add(textBlock);
        m_icon = New Image()
        m_icon.VerticalAlignment = VerticalAlignment.Center
        m_icon.Margin = New Thickness(0, 0, 4, 0)
        m_icon.Source = iconSource

        stack.Children.Add(m_icon)
        'Add the HeaderText After Adding the icon
        textBlock = New TextBlock()
        textBlock.VerticalAlignment = VerticalAlignment.Center
        stack.Children.Add(textBlock)
    End Sub
#End Region
#Region "Properties"
    ''' <summary>
    ''' Gets/Sets the Selected Image for a TreeViewNode
    ''' </summary>
    Public Property Icon() As ImageSource
        Get
            Return iconSource
        End Get
        Set(value As ImageSource)
            iconSource = value
            m_icon.Source = iconSource
        End Set
    End Property
#End Region
#Region "Event Handlers"
    ''' <summary>
    ''' Event Handler on UnSelected Event
    ''' </summary>
    ''' <param name="args">Eventargs</param>
    Protected Overrides Sub OnUnselected(args As RoutedEventArgs)
        MyBase.OnUnselected(args)
        m_icon.Source = iconSource
    End Sub
    ''' <summary>
    ''' Event Handler on Selected Event 
    ''' </summary>
    ''' <param name="args">Eventargs</param>
    Protected Overrides Sub OnSelected(args As RoutedEventArgs)
        MyBase.OnSelected(args)
        m_icon.Source = iconSource
    End Sub

    ''' <summary>
    ''' Gets/Sets the HeaderText of TreeViewWithIcons
    ''' </summary>
    Public Property HeaderText() As String
        Get
            Return textBlock.Text
        End Get
        Set(value As String)
            textBlock.Text = value
        End Set
    End Property
#End Region
End Class


Friend Module IconUtilities
  
    <DllImport("gdi32.dll", SetLastError:=True)> _
    Private Function DeleteObject(hObject As IntPtr) As Boolean
    End Function

    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToImageSource(bitmap As System.Drawing.Bitmap) As ImageSource

        Dim hBitmap As IntPtr = bitmap.GetHbitmap()

        Dim wpfBitmap As ImageSource = Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())

        If Not DeleteObject(hBitmap) Then
            Throw New Win32Exception()
        End If

        Return wpfBitmap
    End Function
End Module

